package com.greenant.store.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.greenant.store.AntHomeApplication;
import com.greenant.store.R;
import com.greenant.store.bean.AntGoods;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GoodsListAdapterForImageItem extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private DecimalFormat mDecimalFormat;
	
	private List<AntGoods> mDataList;
	
	private boolean isDisplayMarketPrice = false;
	
	public GoodsListAdapterForImageItem(Context context, List<AntGoods> dataList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(mContext);
		mDataList = dataList;
		mDecimalFormat = new DecimalFormat("#########.##");
	}

	public void setDisplayMarketPrice(boolean display) {
		isDisplayMarketPrice = display;
	}
	
	@Override
	public int getCount() {
		if(mDataList != null) {
			return mDataList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View child = convertView;
		ViewHolder holder = null;
		if(child == null) {
			child = mLayoutInflater.inflate(R.layout.goods_list_for_image_item, null);
			holder = new ViewHolder(child);
			child.setTag(holder);
		}
		else {
			holder = (ViewHolder)child.getTag();
		}
		
		AntGoods goods = mDataList.get(position);
		ImageLoader.getInstance().displayImage(goods.getImageUrl(), holder.getIvPic(),AntHomeApplication.imageDisplayOptions);
		holder.getTvDes().setText(goods.getDescriptionAndSpecs());
		holder.getTvPrice().setText(mDecimalFormat.format(goods.getRealPrice()));
		holder.getTvUnit().setText(" / " + goods.getUnit());
		holder.getTvStore().setText(mDecimalFormat.format(goods.getStock()));
		
		if(isDisplayMarketPrice) {
			double marketPrice = goods.getPrice();
			marketPrice = (marketPrice<0 ? 0 : marketPrice);
			holder.getTvMarketPrice().getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
			holder.getTvMarketPrice().setText(String.valueOf(marketPrice));
			holder.getTvMarketPrice().setVisibility(View.VISIBLE);
		}
		else{
			holder.getTvMarketPrice().setVisibility(View.GONE);
		}
		
		if(goods.isRecommend()) {
			holder.getShowTag().setVisibility(View.VISIBLE);
			holder.getShowTag().setBackgroundResource(R.drawable.show_tag_recommend);
		} else {
			holder.getShowTag().setVisibility(View.INVISIBLE);
		}
		holder.getShowTagSoldOut().setVisibility(View.GONE);
		
		return child;
	}
	
	private class ViewHolder {
		private View mContentView;
		private ImageView mIvPic;
		private View mShowTag;
		private View mShowTagSoldOut;
		private TextView mTvDes;
		private TextView mTvPrice;
		private TextView mTvUnit;
		private TextView mTvMarketPrice;
		private TextView mTvStore;
        
		public ViewHolder(View contentView) {
			mContentView = contentView;
		}
		
		public View getShowTag() {
			if(mShowTag == null) {
				mShowTag = mContentView.findViewById(R.id.goods_list_item_pic_show_tag);
			}
			return mShowTag;
		}
		
		public View getShowTagSoldOut() {
			if(mShowTagSoldOut == null) {
				mShowTagSoldOut = mContentView.findViewById(R.id.goods_list_item_pic_show_tag_sold_out);
			}
			return mShowTagSoldOut;
		}

		public ImageView getIvPic() {
			if(mIvPic == null) {
				mIvPic = (ImageView)mContentView.findViewById(R.id.goods_list_item_pic);
			}
			return mIvPic;
		}

		public TextView getTvDes() {
			if(mTvDes == null) {
				mTvDes = (TextView)mContentView.findViewById(R.id.goods_list_item_des);
			}
			return mTvDes;
		}

		public TextView getTvPrice() {
			if(mTvPrice == null) {
				mTvPrice = (TextView)mContentView.findViewById(R.id.goods_list_item_price);
			}
			return mTvPrice;
		}

		public TextView getTvUnit() {
			if(mTvUnit == null) {
				mTvUnit = (TextView)mContentView.findViewById(R.id.goods_list_item_unit);
			}
			return mTvUnit;
		}
		
		public TextView getTvMarketPrice() {
			if(mTvMarketPrice == null) {
				mTvMarketPrice = (TextView)mContentView.findViewById(R.id.goods_list_item_market_price);
			}
			return mTvMarketPrice;
		}
		
		public TextView getTvStore() {
			if(mTvStore == null) {
				mTvStore = (TextView)mContentView.findViewById(R.id.goods_list_item_store_count);
			}
			return mTvStore;
		}
	}

}








